package com.hsb.seckill.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hsb.seckill.dao.SeckillOrderDao;
import com.hsb.seckill.entity.SeckillOrder;
import com.hsb.seckill.entity.User;
import com.hsb.seckill.service.SeckillOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * (SeckillOrder)表服务实现类
 *
 * @author makejava
 * @since 2021-09-05 21:59:33
 */
@Service("seckillOrderService")
public class SeckillOrderServiceImpl extends ServiceImpl<SeckillOrderDao, SeckillOrder> implements SeckillOrderService {
    @Resource
    private SeckillOrderDao seckillOrderDao;
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 获取秒杀结果
     * orderId：成功，-1:失败，0:排队中
     * @param user
     * @param goodsId
     * @return
     */
    @Override
    public Long getResult(User user, Long goodsId) {
        SeckillOrder seckillOrder = seckillOrderDao.selectOne(new QueryWrapper<SeckillOrder>()
                .eq("user_id", user.getId()).eq("goods_id", goodsId));

        if (null != seckillOrder){
            return seckillOrder.getOrderId();
        }else if (redisTemplate.hasKey("isStockEmpty:"+goodsId)){
            return -1L;
        }else {
            return 0L;
        }
    }
}